package com.app.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.app.mapper.AdminMapMapper;
import com.app.model.ArticleModel;
import com.app.model.CategoryModel;
import com.app.model.MapModel;
import com.app.model.RoleModel;
import com.app.service.AdminMapService;
import com.app.service.CategoryService;
import com.app.vo.MapVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminMapServiceImpl implements AdminMapService {
    @Autowired
    private AdminMapMapper adminMapMapper;
    @Autowired
    private CategoryService categoryService;

    @Override
    public void update(MapModel mapModel) {
        adminMapMapper.updateById(mapModel);
    }

    @Override
    public void insert(MapModel mapModel) {
        adminMapMapper.insert(mapModel);
    }

    @Override
    public void deleteById(Integer id) {
        adminMapMapper.deleteById(id);
    }

    @Override
    public MapVo selectList(Integer page, Integer limit, String keyword, Integer type, Integer status) {
        QueryWrapper<MapModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "longitude", "latitude","keywords", "address", "mobile","status", "icon", "type", "create_time", "update_time");
        if (keyword != null) {
            queryWrapper.like("name", keyword);
        }
        if (type != null && type != 0) {
            queryWrapper.eq("type", type);
        }
        Page<MapModel> page1 = new Page<>();
        page1.setCurrent(page);
        page1.setSize(limit);
        Page<MapModel> mapModelPage = adminMapMapper.selectPage(page1, queryWrapper);
        List<CategoryModel> categoryByPid = categoryService.getCategoryByPid(23);
        for (int i = 0; i < mapModelPage.getRecords().size(); i++) {
            for (int i1 = 0; i1 < categoryByPid.size(); i1++) {
                if (mapModelPage.getRecords().get(i).getType() != null && mapModelPage.getRecords().get(i).getType().equals(categoryByPid.get(i1).getId())) {
                    mapModelPage.getRecords().get(i).setTypeText(categoryByPid.get(i1).getName());
                }
            }
        }
        MapVo vo = new MapVo();
        vo.setList(mapModelPage.getRecords());
        vo.setCount((int) mapModelPage.getTotal());
        return vo;
    }

    @Override
    public MapModel selectById(Integer id) {
        return adminMapMapper.selectById(id);
    }

    @Override
    public List<MapModel> getList(String keywords) {
        QueryWrapper<MapModel> mapModelQueryWrapper=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(keywords))
        {
            mapModelQueryWrapper.like("keywords",keywords);
        }
        return adminMapMapper.selectList(mapModelQueryWrapper);
    }
}
